<?php
class ContentAction extends BaseAction{
	  
	  private $catModel;
    function _initialize() {
    	parent::_initialize();
    	$this->catModel = D('Cat');
    }
    
    //内容列表
    public function contentList(){
    	
    	$type = 'News';
    	if($_GET['type']){
    		$type = $_GET['type'];
    	}
    	
    	$cat = M("Cat"); // 实例化 
    	$t = C("DB_PREFIX"); //表前缀
    	$table =  $t.'content';//联查表名
    	$field =  'tc.id,tc.title,tc.ctime,tc.uname,tc.istop,tc.isrecom,tc.status,cname'; //查询字段
    	$where =  'type="'.$type.'"'; //查询条件
    	$map['type'] = $type;
    	if($_GET['cat']){
    		 $map['tc.cat'] = $_GET['cat'];
    	}
    	if($_GET['keyword']){
    		 $map['tc.title'] = array('like','%'.$_GET['keyword'].'%');
    	}
    	$pagemb = '';
    	if($type == 'Spage'){
    		 $table =  $t.'spage';
    		 $field =  'tc.id,tc.ctime,tc.uname,tc.status,cname'; //查询字段
    		 $where =  '1=1'; //查询条件
    		 $pagemb = 'spageList';
    	}
      import("ORG.Util.Page"); // 导入分页类   
      $count = $cat->join('RIGHT JOIN '.$table.' as tc ON '.$t.'cat.id = tc.cat')->field('tc.id')->where($map)->count(); // 查询满足要求的总记录数   
      $Page  = new Page($count,10); // 实例化分页类 传入总记录数和每页显示的记录数   
      $show  = $Page->show(); // 分页显示输出   
      $list  = $cat->join('RIGHT JOIN '.$table.' as tc ON '.$t.'cat.id = tc.cat')->field($field)->where($map)->order('tc.ctime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
      
          		 //封装栏目下拉框
    		 $cat = '<select name="cat" id="cat">'.
					  '<option value="0">请选择分类</option>';

    	   $cdata = $this->catModel->getTypeCats($type);
    	
		    	foreach($cdata as $cd){
		    		if($cd['id']==$_GET['cat']){
			    			 $cat .=   '<option value="'.$cd['id'].'" selected="selected">';
			      }else{
			      	   $cat .=   '<option value="'.$cd['id'].'">';
			      }
			           
			    	for($i=0;$i<$cd['depth'];$i++) $cat .= '--';
			    	$cat .= ' '.$cd['cname'].'</option>';
		 
		    	}
    	   $cat .=  '</select>';
    	$this->assign("cats",$cat);
         
      $this->assign('list',$list); // 赋值数据集   
      $this->assign('page',$show); // 赋值分页输出
      $this->assign('type',$type);
      $this->assign('keyword',$_GET['keyword']);
      $this->assign('models',D('Model')->getModel());   
      $this->display($pagemb); // 输出模板   

    }
    
    //添加基本内容
    public function addContent(){
    	if($_GET['id']){
    		 $content = M("Content")->find($_GET['id']);
    		 $this->assign("content",$content);
    	}
    	$this->assign("models",D('Model')->getModel());
    	$this->display();
    }

    //修改基本内容
    public function editContent(){
    	if($_GET['id']){
    		 $content = M("Content")->find($_GET['id']);
    		 $strarr = explode(";",$content['style']);
    		 if(is_array($strarr) && count($strarr)>=4){
    		 	 $content['isStrong'] = $strarr[0];
    		 	 $content['isi'] = $strarr[1];
    		 	 $content['iss'] = $strarr[2];
    		 	 $content['fontColor'] = $strarr[3];
    		 }
    		 $model = D('Model')->getModelByTable($content['model']);
    		 
    		 //封装栏目下拉框
    		 $cat = '<select name="cat" id="cat">'.
					  '<option value="0">请选择分类</option>';

    	   $cdata = $this->catModel->getTypeCats($content['model']);
    	
		    	foreach($cdata as $cd){
		    		if($cd['id']==$content['cat']){
			    			 $cat .=   '<option value="'.$cd['id'].'" selected="selected">';
			      }else{
			      	   $cat .=   '<option value="'.$cd['id'].'">';
			      }
			           
			    	for($i=0;$i<$cd['depth'];$i++) $cat .= '--';
			    	$cat .= ' '.$cd['cname'].'</option>';
		 
		    	}
    	   $cat .=  '</select>';
    	   $this->assign("cats",$cat);
    		 $this->assign("content",$content);
    		 $this->assign("model",$model);
    	}
    	$this->display();
    }
    
    
    
    //添加基本内容
    private function _addContent(){
    	$content = D('Content');
    	$cid = 0;
    	if($content->create()){
    		  $cat = $this->catModel->getCatById($content->cat);
    		  $temp['width'] = $cat['maxwidth']."px";
    		  if($_POST['num'] == 0)
    			$content->num = rand($cat['bnum'],$cat['enum']);  //添加内容默认点击数
					$content->ctime = time(); //发布时间 
					$content->uid   = $_SESSION['adminId'];      //发布者ID
					$content->uname = $_SESSION['account'];      //发布者账号
					$content->model=$cat['type'];
					$content->status = 0;                        //状态默认为0
					$content->style = $_POST['isStrong'].';'.$_POST['isi'].';'.$_POST['iss'].';'.$_POST['fontColor'];
					$info = upload('content',$cat['picwidth'],$cat['picheight']); //上传图片
					if($info){
						$content->img = 'content/thumb_'.$info[0]['savename'];
				  }
					$cid = $content->add();
			}else{
					$this->error($content->getError());
					return false;
			}
			
			$temp['id'] = $cid;
			$temp['title'] = $content->title;
			$temp['type'] = $_POST['type'];
			$temp['cat'] = $_POST['cat'];
			return $temp;
    }
    
    //添加
    public function doAddContent(){
    	  $content = D($_POST['type']);
    	  if($content->create()){
    	  	$content->style = $_POST['isStrong'].';'.$_POST['isi'].';'.$_POST['iss'].';'.$_POST['fontColor'];
	    		$this->assign("jumpUrl","__URL__/contentList/type/".$_POST['type']);
	    		//$content->content = showHtml($_POST['content']);
	    		if($content->add()){
	    			 $this->success('添加内容成功！');
	    		}else{
	    			 $this->error('添加内容失败！');
	    		}
	    	}else{
	    		$this->error('添加内容失败！');
	    	}
    	
    }
    //添加
    public function doNextContent(){
    	  $content = D($_POST['type']);
    	  if($content->create()){
	    		$this->assign("jumpUrl","__URL__/contentList/type/".$_POST['type']);
	    		//$content->content = showHtml($_POST['content']);
	    		   $content->save();
	    			 $this->success('修改内容成功！');
	    		//}else{
	    			 //$this->error('修改内容失败！');
	    		//}
	    	}else{
	    		$this->error('修改内容失败！');
	    	}
    	
    }
    
    //添加其他内容
    public function nextContent(){
    	
    	$content = $this->_addContent();

    	if($content['id']>0){
    		 $this->assign("content",$content);
    		 $this->display("add".$_POST['type']);
    	}else{
    		$this->error('添加内容失败！');
    	}
    }
    
    //修改基本内容
    public function doEditContent(){
 
    	$content = D('Content');
    	
    	if($content->create()){
    		  $cat = $this->catModel->getCatById($content->cat);
    		  $temp['width'] = $cat['maxwidth']."px";
    		  if($_POST['num'] == 0)
    			$content->num = rand($cat['bnum'],$cat['enum']);  //添加内容默认点击数
					$info = upload('content',$cat['picwidth'],$cat['picheight']); //上传分类图片
					$content->style = $_POST['isStrong'].';'.$_POST['isi'].';'.$_POST['iss'].';'.$_POST['fontColor'];
					if($info){
						$content->img = 'content/thumb_'.$info[0]['savename'];
				  }
				  if($content->istop==null){
				  	$content->istop = 0;
				  }
				  if($content->isrecom==null){
				  	$content->isrecom = 0;
				  }
				  if($content->isimg==null){
				  	$content->isimg = 0;
				  }
					$content->save();
					$temp['id'] = $content->id;
					$temp['title'] = $content->title;
					$temp['type'] = $_POST['type'];
					$this->assign("content",$temp);
					
					//判断加载页面
					$nextContext = M($_POST['type'])->find($content->id);
					if($nextContext){
						 $this->assign("nextContent",$nextContext);
						 $this->display("edit".$_POST['type']);
					}else{
    		  	$this->display("add".$_POST['type']);
    			}
			}else{
					$this->error('非法操作！');
					return false;
			}
    
    }
   //审核多条记录
   public function doAudit()
	 {
			//删除指定记录
			$model = M('Content');
			if(!empty($model)) {
				$id = $_REQUEST['id'];
				if(isset($id)) {
					if($model->where('id In('.$id.')')->setField('status',1)){
						$this->assign("jumpUrl", "__URL__/contentList/type/".$_GET['type']);
						$this->success('操作成功！');
					}else {
						$this->error("操作失败!");
					}
				}else {
					$this->error('非法操作');
				}
			}
	 }
	 
	 
	 public function doTop()
	 {
	 		$value = 0;
			if($_GET['ist']){
				$value = $_GET['ist'];
			}
			$model = M('Content');
			if(!empty($model)) {
				$id = $_REQUEST['id'];
				if(isset($id)) {
					if($model->where('id In('.$id.')')->setField('istop',$value)){
						$this->assign("jumpUrl", "__URL__/contentList/type/".$_GET['type']);
						$this->success('操作成功！');
					}else {
						$this->error("操作失败!");
					}
				}else {
					$this->error('非法操作');
				}
			}
	 }
	 
	 public function doRecom()
	 {
			$model = M('Content');
			$value = 0;
			if($_GET['ist']){
				$value = $_GET['ist'];
			}
			if(!empty($model)) {
				$id = $_REQUEST['id'];
				if(isset($id)) {
					if($model->where('id In('.$id.')')->setField('isrecom',$value)){
						$this->assign("jumpUrl", "__URL__/contentList/type/".$_GET['type']);
						$this->success('操作成功！');
					}else {
						$this->error("操作失败!");
					}
				}else {
					$this->error('非法操作');
				}
			}
	 }
	 
	 

    //删除多条记录
   public function delete()
	 {
			//删除指定记录
			$model = M('Content');
			if(!empty($model)) {
				$id = $_REQUEST['id'];
				if(isset($id)) {
					if($model->where('id In('.$id.')')->delete()){
						$nextModel = M($_GET['type']);
						if(!empty($nextModel)){
							 if($nextModel->where('id In('.$id.')')->delete()){
							 		$this->assign("jumpUrl","__URL__/contentList/type/".$_GET['type']);
									$this->success('删除成功！');
							 }else {
									$this->error("删除失败!");
							 }
						}else {
							$this->error('非法操作');
						}
						
						

					}else {
						$this->error("删除失败!");
					}
				}else {
					$this->error('非法操作');
				}
				$this->forward();
			}
	 }

   
}
?>